﻿using Npgsql;
using System;
using System.Data;
using System.Windows;

namespace DXApplicationTangche.Utils
{
    public class SQLreport
    {
        public static string ConnStr = "server=192.168.3.22;port=5432;user id=postgres;password=Win2003@;database=data_report";//   内网
        //public static string ConnStr = "server=175.170.138.171;port=6432;user id=postgres;password=Win2003@;database=data_report";// 外网

        public static void ExecuteSql(String sql)
        {
            NpgsqlConnection AConn = new NpgsqlConnection(ConnStr);
            NpgsqlCommand cmd = new NpgsqlCommand(sql, AConn);
            AConn.Open();
            try
            {
                int s = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("当前时间：" + DateTime.Now.ToString() + "\n" +
                            "异常信息：" + ex.Message + "\n" +
                            "异常对象：" + ex.Source + "\n" +
                            "调用堆栈：\n" + ex.StackTrace.Trim() + "\n" +
                            "触发方法：" + ex.TargetSite + "\n");
            }
            finally
            {
                AConn.Close();
                AConn.Dispose();
                AConn = null;
            }
        }

        public static DataTable GetDataTable(string SQL)
        {
            DataTable ADt = new DataTable();
            NpgsqlConnection AConn = new NpgsqlConnection(ConnStr);
            try
            {
                NpgsqlDataAdapter ADp = new NpgsqlDataAdapter(SQL, AConn);
                ADp.Fill(ADt);
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误信息: " + ex.Message);
            }
            finally
            {
                AConn.Close();
                AConn.Dispose();
                AConn = null;
            }
            return ADt;
        }
    }
}
